<?php

/**
 * @author vladone
 * @copyright 2007
 */
//include ('include/conf.php');

function removeFile($target_path)
{
    //when finish delete file. Se verifica daca exista (paranoia))
    if (file_exists($target_path))
    {
        if (!unlink($target_path))
        {
            echo ("eroare la stergerea fisierului");
        }
    }
}

function send_mail($to, $body, $subject, $fromaddress, $fromname, $attach = false)
{
	$eol="\n";
	$semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
	
	$headers .= "From: ".$fromname."<".$fromaddress.">".$eol;
	$headers .= "MIME-Version: 1.0".$eol;
	$headers .= "Content-Type: multipart/mixed;".$eol." boundary=\"{$mime_boundary}\"";;
	
	$email_message .= "This is a multi-part message in MIME format.\n\n" .
	"--{$mime_boundary}\n" .
	"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
	"Content-Transfer-Encoding: 7bit\n\n" .
	$body . "\n\n";
	
	if($attach != false){
		//atachament
		$fileatt = $attach['file']; // Path to the file
		$fileatt_type = "application/octet-stream"; // File Type
		$fileatt_name = substr($attach['file'], (strrpos($attach['file'], "/")+1));;
		if(($file = fopen($fileatt,'rb')) === FALSE){
			die('Failed to open file!');
		}
		$data = fread($file,filesize($fileatt));
		fclose($file);
	
		$data = chunk_split(base64_encode($data));
	
		$email_message .= "--{$mime_boundary}\n" .
		"Content-Type: {$fileatt_type};\n" .
		" name=\"{$fileatt_name}\"\n" .
		//"Content-Disposition: attachment;\n" .
		//" filename=\"{$fileatt_name}\"\n" .
		"Content-Transfer-Encoding: base64\n\n" .
		$data . "\n\n" .
		"--{$mime_boundary}\n";
	}
    $mail_sent = mail($to, $subject, $email_message, $headers);
    return $mail_sent;
}

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/filestore/" . basename($_FILES['Filedata']['name']);
if (!move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path))
{
    echo ("fisierul nu poate fi mutat!");
    exit();
}
$subject = "RPL File -- simple html text";
$mailTo = "vladone_2004@yahoo.com";
$mailFrom = "codigo@imeiserver.es";
$fromUser = "vlad";
$message = "RPL File pentru Nokia<br><strong>Sa vedem daca merge Html</strong>";
$attach = array('file' => $target_path);
$mailSend = send_mail($mailTo, $message, $subject, $mailFrom, $fromUser, $attach);
echo $mailSend;

removeFile($target_path);
?>